package com.dongbing.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.charset.Charset;
import java.util.Iterator;

/**
 * netty 写模式
 */
public class NettyWriteServer {
    public static void main(String[] args) throws IOException {
        // 创建客户端
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        // 设置不堵塞
        serverSocketChannel.configureBlocking(false);
        // 绑定端口
        serverSocketChannel.bind(new InetSocketAddress(8000));
        // 设置监视器
        Selector selector = Selector.open();
        // 监视 ServerSocketChannel
        SelectionKey selectionKey = serverSocketChannel.register(selector, 0, null);
        // 监控 ServerSocketChannel 对什么事件感兴趣 新建连接
        selectionKey.interestOps(SelectionKey.OP_ACCEPT);

        while (true) {
            // 开始监视
            selector.select();
            // selectedKeys 是每一次变动的 Channel 都会放到这个里面来
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                // 这里要注意 我们获取到那个那个变动了 那么就
                iterator.remove();
                // 查看是什么事件
                if (key.isAcceptable()) {
                    // 获取服务端channel
                    ServerSocketChannel channel = (ServerSocketChannel) key.channel();
                    // 拿到客户端channel
                    SocketChannel socketChannel = channel.accept();
                    // 设置不堵塞
                    socketChannel.configureBlocking(false);
                    // 设置监视器
                    SelectionKey register = socketChannel.register(selector, 0, null);
                    // 设置监视读模式
                    register.interestOps(SelectionKey.OP_READ);

                    StringBuilder append = new StringBuilder();
                    for (int i = 0; i < 2000000; i++) {
                        append.append("DB");
                    }

                    // 回写数据
                    ByteBuffer buffer = Charset.defaultCharset().encode(append.toString());
                    int write = socketChannel.write(buffer);
                    System.out.println("write = " + write);

                    if (buffer.hasRemaining()) {
                        // 新增write监听
                        register.interestOps(register.interestOps() + SelectionKey.OP_WRITE);
                        // 将剩余的buffer放到附件里面
                        selectionKey.attach(buffer);
                    }
                } else {
                    SocketChannel socketChannel = (SocketChannel) key.channel();
                    ByteBuffer buffer = (ByteBuffer) selectionKey.attachment();

                    int write = socketChannel.write(buffer);
                    System.out.println("write = " + write);

                    if (!buffer.hasRemaining()) {
                        key.attach(null);
                        key.interestOps(key.interestOps() - SelectionKey.OP_WRITE);
                    }
                }
            }
        }
    }
}
